﻿using System;
using System.Collections.Generic;

namespace Program18
{
    class Program
    {
        static void Main(string[] args)
        {
           //用方法实现：计算1-100之间的质数的和
           //质数就是只能被1和它本身整除的整数2,3,5,7,11,13,17,19,23,29,31,37,41,43

           //while (true)
           //{
           //    Console.WriteLine("请输入一个数：");
           //    int n = Convert.ToInt32(Console.ReadLine());
           //    bool b = IsPrime(n);
           //    Console.WriteLine(b?"质数":"非质数");
           //}
           Console.WriteLine("请输入一个数：");
           int n = Convert.ToInt32(Console.ReadLine());
            var list=new List<int>();
            var sum = GetPrimeSum(n, out list);
            foreach (var item in list)
            {
                Console.Write("{0}",item+" ");
            }
            Console.WriteLine("\r\n总和{0}",sum);


        }

        /// <summary>
        /// 判断是否为质数
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        public static bool IsPrime(int n)
        {
            //1不算质数
            if (n < 2)
            {
                return false;
            }
            else
            //>=情况
            {
                for (int i = 2; i <= n/2; i++)
                {
                    if (n % i == 0)
                        return false;
                }
                return true;
            }
            
        }

        public static int GetPrimeSum(int n,out List<int> list)
        {
            list=new List<int>();
            int sum = 0;
            for (int i = 2; i < n; i++)
            {
                if (IsPrime(i))
                {
                    list.Add(i);
                    sum += i;
                }
            }
            return sum;
        }
    }
}
